import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import 'post_model.dart';

main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: FutureBuilder<Post>(
          future: fetchPost(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('${snapshot.data.userId}'),
                  Text('${snapshot.data.id}'),
                  Text('${snapshot.data.title}'),
                  Text('${snapshot.data.body}'),
                ],
              );
            } else if (snapshot.hasError) {
              return Text("${snapshot.error}");
            }
            return CircularProgressIndicator();
          },
        ),
      ),
    );
  }

  Future<Post> fetchPost() async {
    final response =
        await Dio().get('https://jsonplaceholder.typicode.com/posts/1');
    return Post.fromJson(response.data);
  }
}
